﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ISHotelApp.Database;
using ISHotelApp.XML;

namespace ISHotel_WinForm
{
    public partial class HosteForm : Form
    {
        public HosteForm()
        {
            InitializeComponent();

            HostTable hostTable = new HostTable();
            dataGridViewHost.AutoGenerateColumns = false;

            DataGridViewTextBoxColumn rodneCisloColumn = new DataGridViewTextBoxColumn();
            rodneCisloColumn.DataPropertyName = "rodne_cislo";
            rodneCisloColumn.HeaderText = "Rodne cislo";
            DataGridViewTextBoxColumn jmenoColumn = new DataGridViewTextBoxColumn();
            jmenoColumn.DataPropertyName = "jmeno";
            jmenoColumn.HeaderText = "Jmeno";
            DataGridViewTextBoxColumn prijmeniColumn = new DataGridViewTextBoxColumn();
            prijmeniColumn.DataPropertyName = "prijmeni";
            prijmeniColumn.HeaderText = "Prijmeni";
            DataGridViewTextBoxColumn uliceColumn = new DataGridViewTextBoxColumn();
            uliceColumn.DataPropertyName = "ulice";
            uliceColumn.HeaderText = "Ulice";
            DataGridViewTextBoxColumn mestoColumn = new DataGridViewTextBoxColumn();
            mestoColumn.DataPropertyName = "mesto";
            mestoColumn.HeaderText = "Mesto";
            DataGridViewTextBoxColumn pscColumn = new DataGridViewTextBoxColumn();
            pscColumn.DataPropertyName = "psc";
            pscColumn.HeaderText = "PSC";
            DataGridViewTextBoxColumn telefonColumn = new DataGridViewTextBoxColumn();
            telefonColumn.DataPropertyName = "telefon";
            telefonColumn.HeaderText = "Telefon";
            DataGridViewTextBoxColumn emailColumn = new DataGridViewTextBoxColumn();
            emailColumn.DataPropertyName = "email";
            emailColumn.HeaderText = "E-mail";

            dataGridViewHost.DataSource = hostTable.Select();

            dataGridViewHost.Columns.Add(rodneCisloColumn);
            dataGridViewHost.Columns.Add(jmenoColumn);
            dataGridViewHost.Columns.Add(prijmeniColumn);
            dataGridViewHost.Columns.Add(uliceColumn);
            dataGridViewHost.Columns.Add(mestoColumn);
            dataGridViewHost.Columns.Add(pscColumn);
            dataGridViewHost.Columns.Add(telefonColumn);
            dataGridViewHost.Columns.Add(emailColumn);
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            ErrLabel.Text = "";
            HostTable delSourceRecord = new HostTable();
            delSourceRecord.Delete(RodneCisloTB.Text);
            dataGridViewHost.DataSource = delSourceRecord.Select();
        }

        private void button_update_Click(object sender, EventArgs e)
        {
            ErrLabel.Text = "";
            Host newHost = new Host();
            newHost.rodne_cislo = RodneCisloTB.Text;
            newHost.jmeno = JmenoTB.Text;
            newHost.prijmeni = PrijmeniTB.Text;
            newHost.ulice = UliceTB.Text;
            newHost.mesto = MestoTB.Text;
            newHost.psc = Convert.ToInt32(PSCTB.Text);
            if (TelefonTB.Text == "")
                newHost.telefon = 0;
            else
                newHost.telefon = Convert.ToInt32(TelefonTB.Text);
            if (EmailTB.Text == "")
                newHost.email = "NOT PROVIDED";
            else
                newHost.email = EmailTB.Text;
            HostTable addSourceRecord = new HostTable();

            try
            {
                addSourceRecord.Update(newHost);
                dataGridViewHost.DataSource = addSourceRecord.Select();
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().IndexOf("ORA-00001") >= 0) { ErrLabel.Text = "Host s tímto rodným číslem již existuje."; }
                else { ErrLabel.Text = ex.Message.ToString(); };
            }
        }

        private void button_insert_Click(object sender, EventArgs e)
        {
            ErrLabel.Text = "";
            Host newHost = new Host();
            newHost.rodne_cislo = RodneCisloTB.Text;
            newHost.jmeno = JmenoTB.Text;
            newHost.prijmeni = PrijmeniTB.Text;
            newHost.ulice = UliceTB.Text;
            newHost.mesto = MestoTB.Text;
            newHost.psc = Convert.ToInt32(PSCTB.Text);
            if (TelefonTB.Text == "")
                newHost.telefon = 0;
            else
                newHost.telefon = Convert.ToInt32(TelefonTB.Text);
            if (EmailTB.Text == "")
                newHost.email = "NOT PROVIDED";
            else
                newHost.email = EmailTB.Text;
            HostTable addSourceRecord = new HostTable();

            try
            {
                addSourceRecord.Insert(newHost);
                dataGridViewHost.DataSource = addSourceRecord.Select();
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().IndexOf("ORA-00001") >= 0) { ErrLabel.Text = "Host s tímto rodným číslem již existuje."; }
                else { ErrLabel.Text = ex.Message.ToString(); };
            }

        }

        private void dataGridViewHost_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            ErrLabel.Text = "";
            if (e.RowIndex >= 0)
            {
                button_update.Enabled = true;
                button_delete.Enabled = true;
                string rodne_cislo = dataGridViewHost.Rows[e.RowIndex].Cells[0].Value as string;
                RodneCisloTB.Text = rodne_cislo;
                HostTable hostTable = new HostTable();
                Host host = hostTable.Select(rodne_cislo);
                JmenoTB.Text = host.jmeno;
                PrijmeniTB.Text = host.prijmeni;
                UliceTB.Text = host.ulice;
                MestoTB.Text = host.mesto;
                PSCTB.Text = host.psc.ToString();
                TelefonTB.Text = host.telefon.ToString();
                EmailTB.Text = host.email;
            }
        }

        private void exportBut_Click(object sender, EventArgs e)
        {
            try
            {
                Export2XML.exportHost(dataGridViewHost.DataSource);
                MessageBox.Show("Export OK.");
            }
            catch (Exception ex)
            {
                ErrLabel.Text = "Export se nezdaril.";
            }
        }
    }
}
